# 生成银联对账文件

from libs.db2 import OceanDb
from custException import *
db = OceanDb()


def run():
    # 将需要生成对账文件的交易信息的BUSI_REQ_NO填入下面的列表
    BUSI_REQ_NO_LIST = [2025071837443120]
    for busi_no in BUSI_REQ_NO_LIST:
        data = db.query(f"""select * from BUPPS_CUPS_SERIAL where BUSI_REQ_NO = '{str(busi_no)}'""")
        if not data:
            raise DataNotFoundInDb(f'{busi_no=}')
        f32 = data['REQ_BRANCH'].ljust(11)
        f33 = data['SEND_BRANCH'].ljust(11)
        f11 = data['SYSTEM_TRACK_NO'].ljust(6)
        f7 = data['TRANS_DTTM'].ljust(10)
        f2 = data['MAIN_ACCT'].ljust(19)
        f4 = data['TRANS_AMT_CHAR'].ljust(12)
        f95 = '000000000000'
        f28 = ' 00000000000'
        msg_type = data['MSG_TYPE'].ljust(4)
        f3 = data['THIRD_TRANS_CODE'].ljust(6)
        f18 = data['MERCH_TYPE'].ljust(4)
        f41 = data['TERMINAL_CODE'].ljust(8)
        f42 = data['MERCH_NO'].ljust(15)
        f37 = data['TERM2TERM_ID'].ljust(12)
        f25 = data['SERVICE_COND_CODE'].ljust(2)
        f38 = data['GRANT_CODE'].ljust(6) if data['GRANT_CODE'] else '000000'
        f100 = data['RECV_BRANCH'].ljust(11) if data['RECV_BRANCH'] else '14373020   '
        f90_2 = '000000'
        f39 = '00'
        f22 = data['INPUT_MODE'].ljust(3)
        YSJHF = '000000000000'
        YFJHF = '000000000000'
        ZJQSF = 'C00000000000'
        DSZHBS = '1'
        f23 = data['CARD_SEQ'].ljust(3) if data['CARD_SEQ'] else '000'
        f60_2_2 = '0'
        f60_2_3 = '0'
        f90_3 = '0000000000'
        FKJGBS = '01052900   '
        JYDYBZ = '0'
        f60_2_5 = data['PIN_INPUT_MODE'].ljust(2) if data['PIN_INPUT_MODE'] else '06'
        ECI_CODE = '00'
        FQFKFJSXF = ' 00000000000'
        QTXX = '00000000000000'
        res_list = [f32, f33, f11, f7, f2, f4, f95, f28, msg_type, f3, f18, f41, f42, f37, f25, f38, f100, f90_2, f39,
                    f22,
                    YSJHF, YFJHF, ZJQSF, DSZHBS, f23, f60_2_2, f60_2_3, f90_3, FKJGBS, JYDYBZ, f60_2_5, ECI_CODE,
                    FQFKFJSXF,
                    QTXX]
        res_str = ' '.join(res_list)
        print(res_str)


if __name__ == '__main__':
    run()
